package com.github.aloxc.plugin.restplus.component;

import com.github.aloxc.plugin.restplus.entity.RestConfig;
import com.github.aloxc.plugin.restplus.entity.SettingListenerContext;
import com.github.aloxc.plugin.restplus.entity.Yapi;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.listener.SettingListener;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBPanel;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static javax.swing.SwingConstants.LEFT;

/**
 * 变量ui
 *
 * @author leero
 */
public class YapiSettingForm extends JBPanel implements ISettingPanel {
    private JPanel rootPanel;
    private JPanel topSplitPanel;
    private JTabbedPane moduleTabbedPanel;
    private JButton cancelButton;
    private JButton submitButton;
    private JPanel buttonSplitPanel;
    private JPanel buttonPanel;
    private JLabel tipLabel;
    private Dialog dialog;
    private Project project;
    private String module;

    private Map<String,List<JTextField>> textFieldMap = new HashMap<>(2<<3);
    public JPanel getRootPanel() {
        return rootPanel;
    }


    @Override
    protected void printComponent(Graphics g) {
        super.printComponent(g);
    }

    @Override
    public void setContext(SettingListenerContext context) {
        module = (String) context.getContext();
    }

    public YapiSettingForm() {
        super();
        initComponent();
    }

    public YapiSettingForm(Project project, SettingListenerContext context) {
        super();
        this.project = project;
        setContext(context);
        initComponent();
    }


    private void initComponent() {
        initUi();
        initAction();
    }

    private void initUi() {
        GridLayoutManager rootLayoutManager = new GridLayoutManager(1, 1);
        rootLayoutManager.setHGap(1);
        rootLayoutManager.setVGap(1);
        rootLayoutManager.setMargin(new Insets(0, 0, 0, 0));
        rootPanel = new JBPanel<>();
        this.setBorder(BorderFactory.createEmptyBorder());
        this.setLayout(new GridLayoutManager(1, 1));

        initPanel();
        initText();

        submitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        cancelButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

    }

    private void initText() {
        this.cancelButton.setText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.yapi.cancel"));
        this.submitButton.setText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.yapi.confirm"));
    }

    private void initTopSplitPanel() {
        FlowLayout layout = new FlowLayout();
        layout.setVgap(1);
        layout.setHgap(1);
        layout.setAlignment(FlowLayout.LEFT);

        topSplitPanel = new JPanel();
        topSplitPanel.setLayout(layout);
        int topHeight = 30;
        Dimension dimension = new Dimension(-1, topHeight);
        tipLabel.setText(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.yapi.tips"));
        topSplitPanel.add(tipLabel);
        tipLabel.setHorizontalAlignment(LEFT);
        rootPanel.add(topSplitPanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_NONE,
                        GridConstraints.FILL_NONE,
                        GridConstraints.FILL_NONE,
                        dimension, dimension, dimension));
    }

    private void initTabbedPanel() {
        Module[] modules = ModuleManager.getInstance(project).getModules();
        GridLayoutManager layoutManager = new GridLayoutManager(1, 1);
        layoutManager.setHGap(1);
        layoutManager.setVGap(1);
        layoutManager.setMargin(new Insets(0, 0, 0, 0));

        if (moduleTabbedPanel.getTabCount() > 0) {
            moduleTabbedPanel.removeAll();
        }
        Dimension labelDimension = new Dimension(100, 35);
        Dimension inputDimension = new Dimension(600, 35);
        Dimension lineDimension = new Dimension(700, 35);
        GridLayoutManager inputLayoutManager = new GridLayoutManager(3, 1);
        inputLayoutManager.setHGap(1);
        inputLayoutManager.setVGap(1);
        inputLayoutManager.setMargin(new Insets(0, 0, 0, 0));

        GridLayoutManager moduleLayoutManager = new GridLayoutManager(3, 1);
        moduleLayoutManager.setHGap(1);
        moduleLayoutManager.setVGap(1);
        moduleLayoutManager.setMargin(new Insets(0, 0, 0, 0));

        GridLayoutManager lineLayoutManager = new GridLayoutManager(1, 2);
        lineLayoutManager.setHGap(1);
        lineLayoutManager.setVGap(1);
        lineLayoutManager.setMargin(new Insets(0, 0, 0, 0));

//        List<Yapi> yapiList = Yapi.getYapiConfigList();
//        Map<String, Yapi> yapiMap = yapiList.stream().collect(Collectors.toMap(k -> k.getModule(), v -> v, (k1, k2) -> k1));
        RestConfig config = RestConfig.loadConfig(project);
        Map<String, Yapi> map = config.getYapiList().stream().collect(Collectors.toMap(k -> k.getModule(), v -> v, (k1, k2) -> k1));

        for (Module tabModule : Arrays.stream(modules).sorted(((o1, o2) -> o1.getName().compareTo(o2.getName()))).collect(Collectors.toList())) {
            Yapi yapi = map.get(tabModule.getName());
            JPanel tModulePanel = new JPanel();
            tModulePanel.setLayout(inputLayoutManager);
            JPanel urlLinePanel = new JPanel();
            urlLinePanel.setLayout(lineLayoutManager);
            urlLinePanel.setPreferredSize(lineDimension);

            JPanel tUrlPanel = new JPanel();
            tUrlPanel.setLayout(inputLayoutManager);

            JLabel tUrlLabel = new JLabel("Yapi Url:");
            tUrlLabel.setHorizontalAlignment(SwingConstants.RIGHT);
            JTextField tUrlTextField = new JTextField();
            tUrlTextField.setName("Url--" + tabModule.getName());

            tUrlTextField.setPreferredSize(inputDimension);
            tUrlTextField.setHorizontalAlignment(LEFT);

            JPanel tUrlLabelPanel = new JPanel();
            tUrlLabelPanel.setPreferredSize(labelDimension);
            tUrlLabelPanel.add(tUrlLabel);
            urlLinePanel.add(tUrlLabelPanel,
                    new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_BOTH,
                            GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                            GridConstraints.SIZEPOLICY_FIXED,
                            labelDimension, labelDimension, labelDimension));
            urlLinePanel.add(tUrlTextField,
                    new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_BOTH,
                            GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                            GridConstraints.SIZEPOLICY_FIXED,
                            inputDimension, inputDimension, inputDimension));

            tModulePanel.add(urlLinePanel,
                    new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_BOTH,
                            GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                            GridConstraints.SIZEPOLICY_FIXED,
                            lineDimension, lineDimension, lineDimension));

            JPanel tTokenPanel = new JPanel();
            tTokenPanel.setLayout(inputLayoutManager);

            JPanel tokenLinePanel = new JPanel();
            tokenLinePanel.setLayout(lineLayoutManager);
            tokenLinePanel.setPreferredSize(lineDimension);


            JLabel tTokenLabel = new JLabel("Token:");
            tTokenLabel.setHorizontalAlignment(SwingConstants.RIGHT);
            JTextField tTokenTextField = new JTextField();
            tTokenTextField.setName("token--" + tabModule.getName());

            tTokenTextField.setPreferredSize(inputDimension);
            tTokenTextField.setHorizontalAlignment(LEFT);

            JPanel tTokenLabelPanel = new JPanel();
            tTokenLabelPanel.setPreferredSize(labelDimension);
            tTokenLabelPanel.add(tTokenLabel);
            tokenLinePanel.add(tTokenLabelPanel,
                    new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_BOTH,
                            GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                            GridConstraints.SIZEPOLICY_FIXED,
                            labelDimension, labelDimension, labelDimension));
            tokenLinePanel.add(tTokenTextField,
                    new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_BOTH,
                            GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                            GridConstraints.SIZEPOLICY_FIXED,
                            inputDimension, inputDimension, inputDimension));

            tModulePanel.add(tokenLinePanel,
                    new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_BOTH,
                            GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                            GridConstraints.SIZEPOLICY_FIXED,
                            lineDimension, lineDimension, lineDimension));

            JPanel tProjectIdPanel = new JPanel();
            tProjectIdPanel.setLayout(inputLayoutManager);

            JPanel ProjectIdLinePanel = new JPanel();
            ProjectIdLinePanel.setLayout(lineLayoutManager);
            ProjectIdLinePanel.setPreferredSize(lineDimension);


            JLabel tProjectIdLabel = new JLabel("Project Id:");
            tProjectIdLabel.setHorizontalAlignment(SwingConstants.RIGHT);
            JTextField tProjectIdTextField = new JTextField();
            tProjectIdTextField.setName("ProjectId--" + tabModule.getName());
            tProjectIdTextField.setPreferredSize(inputDimension);
            tProjectIdTextField.setHorizontalAlignment(LEFT);

            JPanel tProjectIdLabelPanel = new JPanel();
            tProjectIdLabelPanel.setPreferredSize(labelDimension);
            tProjectIdLabelPanel.add(tProjectIdLabel);

            if(yapi != null){
                tUrlTextField.setText(yapi.getUrl());
                tTokenTextField.setText(yapi.getToken());
                tProjectIdTextField.setText(yapi.getProjectId());
            }
            ProjectIdLinePanel.add(tProjectIdLabelPanel,
                    new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_BOTH,
                            GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                            GridConstraints.SIZEPOLICY_FIXED,
                            labelDimension, labelDimension, labelDimension));
            ProjectIdLinePanel.add(tProjectIdTextField,
                    new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_BOTH,
                            GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                            GridConstraints.SIZEPOLICY_FIXED,
                            inputDimension, inputDimension, inputDimension));

            tModulePanel.add(ProjectIdLinePanel,
                    new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_BOTH,
                            GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                            GridConstraints.SIZEPOLICY_FIXED,
                            lineDimension, lineDimension, lineDimension));

            moduleTabbedPanel.addTab(tabModule.getName(), null, tModulePanel);
            if(module != null && module.equals(tabModule.getName())){
                moduleTabbedPanel.setSelectedComponent(tModulePanel);
            }
            JTextField[] arr = { tUrlTextField, tTokenTextField, tProjectIdTextField};
            List<JTextField> textFieldList = java.util.Arrays.asList(arr);
            textFieldMap.put(tabModule.getName(),textFieldList);
        }
        Dimension dimension = new Dimension(-1, -1);

        rootPanel.add(moduleTabbedPanel,
                new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                        GridConstraints.SIZEPOLICY_FIXED,
                        dimension, dimension, dimension));
    }

    private void initButtonSplitPanel() {
        Dimension dimension = new Dimension(-1, 30);

        buttonSplitPanel = new JPanel();
        FlowLayout flowLayout = new FlowLayout();
        flowLayout.setVgap(1);
        flowLayout.setHgap(1);
        flowLayout.setAlignment(FlowLayout.CENTER);
        buttonSplitPanel.setLayout(flowLayout);

        rootPanel.add(buttonSplitPanel,
                new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                        GridConstraints.SIZEPOLICY_FIXED,
                        dimension, dimension, dimension));
    }

    private void initButtonPanel() {
        Dimension dimension = new Dimension(100, 50);

        buttonPanel = new JPanel();
        FlowLayout flowLayout = new FlowLayout();
        flowLayout.setVgap(1);
        flowLayout.setHgap(1);
        flowLayout.setAlignment(FlowLayout.CENTER);
        buttonPanel.setLayout(flowLayout);

        cancelButton.setPreferredSize(dimension);
        submitButton.setPreferredSize(dimension);
        buttonPanel.add(cancelButton);
        buttonPanel.add(submitButton);

        rootPanel.add(buttonPanel,
                new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_GROW | GridConstraints.SIZEPOLICY_WANT_GROW,
                        GridConstraints.SIZEPOLICY_FIXED,
                        null, null, null));
    }

    private void initPanel() {
        rootPanel = new JPanel();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(4, 1);
        gridLayoutManager.setVGap(1);
        gridLayoutManager.setHGap(1);

        rootPanel.setLayout(gridLayoutManager);
        initTopSplitPanel();
        initTabbedPanel();
        initButtonSplitPanel();
        initButtonPanel();
        this.add(rootPanel,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_HORIZONTAL,
                        GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                        GridConstraints.FILL_NONE | GridConstraints.FILL_NONE,
                        null, null, null));
    }

    public Project getProject() {
        return project;
    }


    private void initAction() {
        this.cancelButton.addActionListener((e)->{dialog.setVisible(false);});
        this.submitButton.addActionListener(new YapiConfirmActionListener(this));
    }

    @Override
    public void setDialog(Dialog dialog) {
        this.dialog = dialog;
    }

    @Override
    public void setProject(Project project) {
        this.project = project;
    }

    public JTabbedPane getModuleTabbedPanel() {
        return moduleTabbedPanel;
    }

    public Map<String, List<JTextField>> getTextFieldMap() {
        return textFieldMap;
    }
}
